package com.ycuwq.datepicker.date;

import com.ycuwq.datepicker.WheelPicker;
import ohos.agp.components.AttrSet;
import ohos.app.Context;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 日期选择
 * Created by ycuwq on 17-12-28.
 */
public class DayPicker extends WheelPicker<Integer> {
    /**
     * The constant M min day
     */
    private int mMinDay;
    /**
     * M max day
     */
    private int mMaxDay;

    /**
     * The constant M selected day
     */
    private int mSelectedDay;

    /**
     * The constant M year
     */
    private int mYear;
    /**
     * M month
     */
    private int mMonth;
    /**
     * The constant M max date
     */
    private long mMaxDate;
    /**
     * M min date
     */
    private long mMinDate;
    /**
     * The constant M is set max date
     */
    private boolean mIsSetMaxDate;

    /**
     * The constant M on day selected listener
     */
    private OnDaySelectedListener mOnDaySelectedListener;

    /**
     * Day picker
     *
     * @param context context
     */
    public DayPicker(Context context) {
        this(context, null);
    }

    /**
     * Day picker
     *
     * @param context context
     * @param attrs   attrs
     */
    public DayPicker(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    /**
     * Day picker
     *
     * @param context      context
     * @param attrs        attrs
     * @param defStyleAttr def style attr
     */
    public DayPicker(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setItemMaximumWidthText("00");
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(2);
        setDataFormat(numberFormat);

        mMinDay = 1;
        mMaxDay = Calendar.getInstance().getActualMaximum(Calendar.DATE);
        updateDay();
        mSelectedDay = Calendar.getInstance().get(Calendar.DATE);
        setSelectedDay(mSelectedDay, false);
        setOnWheelChangeListener(new OnWheelChangeListener<Integer>() {
            @Override
            public void onWheelSelected(Integer item, int position) {
                mSelectedDay = item;
                if (mOnDaySelectedListener != null) {
                    mOnDaySelectedListener.onDaySelected(item);
                }
            }
        });
    }


    /**
     * Set month *
     *
     * @param year  year
     * @param month month
     */
    public void setMonth(int year, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(mMaxDate);
        int maxYear = calendar.get(Calendar.YEAR);
        int maxMonth = calendar.get(Calendar.MONTH) + 1;
        int maxDay = calendar.get(Calendar.DAY_OF_MONTH);
        // 如果不判断mIsSetMaxDate，则long 为0，则选择1970-01-01 时会有问题
        if (mIsSetMaxDate && maxYear == year && maxMonth == month) {
            mMaxDay = maxDay;
        } else {
            calendar.set(year, month - 1, 1);
            mMaxDay = calendar.getActualMaximum(Calendar.DATE);
        }
        calendar.setTimeInMillis(mMinDate);
        int minYear = calendar.get(Calendar.YEAR);
        int minMonth = calendar.get(Calendar.MONTH) + 1;
        int minDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (minYear == year && minMonth == month) {
            mMinDay = minDay;
        } else {
            mMinDay = 1;
        }
        updateDay();
        if (mSelectedDay < mMinDay) {
            setSelectedDay(mMinDay, false);
        } else if (mSelectedDay > mMaxDay) {
            setSelectedDay(mMaxDay, false);
        } else {
            setSelectedDay(mSelectedDay, false);
        }
    }

    /**
     * Get selected day int
     *
     * @return the int
     */
    public int getSelectedDay() {
        return mSelectedDay;
    }

    /**
     * Set selected day *
     *
     * @param selectedDay selected day
     */
    public void setSelectedDay(int selectedDay) {
        setSelectedDay(selectedDay, true);
    }

    /**
     * Set selected day *
     *
     * @param selectedDay  selected day
     * @param smoothScroll smooth scroll
     */
    public void setSelectedDay(int selectedDay, boolean smoothScroll) {
        setCurrentPosition(selectedDay - mMinDay, smoothScroll);
        mSelectedDay = selectedDay;
    }

    /**
     * Set max date *
     *
     * @param date date
     */
    public void setMaxDate(long date) {
        mMaxDate = date;
        mIsSetMaxDate = true;
    }

    /**
     * Set min date *
     *
     * @param date date
     */
    public void setMinDate(long date) {
        mMinDate = date;
    }

    /**
     * Set on day selected listener *
     *
     * @param onDaySelectedListener on day selected listener
     */
    public void setOnDaySelectedListener(OnDaySelectedListener onDaySelectedListener) {
        mOnDaySelectedListener = onDaySelectedListener;
    }

    /**
     * Update day
     */
    private void updateDay() {
        List<Integer> list = new ArrayList<>();
        for (int i = mMinDay; i <= mMaxDay; i++) {
            list.add(i);
        }
        setDataList(list);
    }

    /**
     * On day selected listener
     */
    public interface OnDaySelectedListener {
        /**
         * On day selected *
         *
         * @param day day
         */
        void onDaySelected(int day);
    }
}
